package com.example.test0520.activity;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.test0520.R;
import com.example.test0520.api.ResponseCallback;
import com.example.test0520.util.City;
import com.example.test0520.util.CityList;
import com.example.test0520.util.WeatherData;
import org.litepal.LitePal;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;

public class QueryActivity extends BaseActivity {

    private EditText editQuery;
    private TextView queryCancel;
    private ListView queryList;
    private List<CityList> cityLists;
    private List<CityList> data;
    private CityAdapter adapter;


    @Override
    protected int initLayout() {
        return R.layout.activity_query;
    }

    @Override
    protected void initView() {
        editQuery = findViewById(R.id.edit_query);
        queryCancel = findViewById(R.id.query_cancel);
        queryList = findViewById(R.id.query_list);

    }

    @Override
    protected void initData() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        editQuery.setFocusable(true);
        editQuery.requestFocus();
        editQuery.setFocusableInTouchMode(true);
        editQuery.requestFocusFromTouch();
        cityLists = LitePal.findAll(CityList.class);
        setListener();

    }

    private void setListener() {
        editQuery.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                String text = editQuery.getText().toString().trim();
                data = new ArrayList<>();
                for (CityList s :
                        cityLists) {
                    if (text.equals(s.getCity()) || text.equals(s.getDistrict())) {
                        data.add(s);
                    }
                }
                adapter = new CityAdapter();
                queryList.setAdapter(adapter);
                Call<WeatherData> weatherDataCall = network("http://apis.juhe.cn/simpleWeather/")
                        .weatherData(text, "827a80d1658ed0b9055b479f77824e86");

                executeAsync(weatherDataCall, new ResponseCallback<WeatherData>() {
                    @Override
                    public void onSuccess(WeatherData weatherData) {

                    }

                    @Override
                    public void onFailure(Throwable t) {

                    }
                });

                return true;
            }
            return false;
        });

        queryList.setOnItemClickListener((parent, view, position, id) -> {
            new City(data.get(position).getProvince(), data.get(position).getCity(), data.get(position).getDistrict()).save();
            startActivity(new Intent(QueryActivity.this, AddActivity.class));
            finish();
        });

        queryCancel.setOnClickListener(v -> {
            onBackPressed();
        });
    }


    class CityAdapter extends BaseAdapter {

        private TextView cityListText;
        private TextView provinceListText;
        private ImageView img;

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_list_item, parent, false);
            } else {
                v = convertView;
            }
            initView(v);
            for (int i = 0; i < data.size(); i++) {
                Log.d("TAG", "onEditorAction: " + data.get(position).getDistrict());
            }
            img.setImageResource(R.mipmap.add);
            if (data.size() != 0) {
                if (data.get(position).getDistrict() != null) {
                    cityListText.setText(data.get(position).getDistrict());
                    provinceListText.setText(data.get(position).getProvince() + "省 " + data.get(position).getCity() + "市 " + data.get(position).getDistrict());
                } else {
                    cityListText.setText(data.get(position).getCity() + "市");
                    provinceListText.setText(data.get(position).getProvince() + "省 " + data.get(position).getCity() + "市 ");
                }
            }
            return v;
        }

        private void initView(View v) {
            img = v.findViewById(R.id.list_add);
            cityListText = (TextView) v.findViewById(R.id.city_list_text);
            provinceListText = (TextView) v.findViewById(R.id.province_list_text);
        }
    }
}